package com.ftg.learn;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author kangjx
 * @createTime 2023/1/10
 */
public class MqConsumer {

    public static void main(String[] args) throws IOException, TimeoutException {
        String queueName = "first";
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5673);

        Connection connect = connectionFactory.newConnection();

        Channel channel = connect.createChannel();
        //在消费者当中用来确认 队列是否存在，如果存在直接执行，不存在不报错误，等待创建队列以 等待消费数据
        channel.queueDeclare(queueName, true, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        /**
         * 回调方法
         */
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");

//            Jsonobject info = JSON.tojson(message);
//            sms  调用sms 给telphone这个电话发送短信，内容为message里面的信息

            System.out.println(" [x] Received '" + message + "'");
        };

        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
            System.out.println(consumerTag);
        });
    }

    //vhost  虚拟机  /
    // 启动消费者的前提是，我们的队列要存在

}
